package com.content.view.Component.OnlineRes
{
	import com.content.model.LoadedAssets.FontProxy;
	import com.content.model.OnlineRes.OnlineResVO;
	import com.content.view.Component.Button.TextButtonColor;
	import com.content.view.Frame.MainPanel.SPBase;
	import com.library.utils.TextFieldUtil;
	
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	public class SPResDetail extends SPBase
	{
		public static const DOWNLOAD:String = "SPResDetail-Audio-Download";
		public static const CLOSE:String = "SPResDetail-Audio-Close";
		
		private var _coverWidth:int = 90;
		private var _coverHeight:int = 120;
		private var _vo:OnlineResVO;
		
		private var _container:Sprite;
		private var _areaBG:Shape;
		private var _coverLo:Loader;	// 90*120
		private var _txtTitle:TextField;
		private var _txtAuthor:TextField;
		private var _txtPublisher:TextField;
		private var _txtPublishDate:TextField;
		private var _txtStar:TextField;
		private var _lblBrief:TextField;
		private var _txtBrief:TextField;
		
		private var _btnDown:TextButtonColor;
		private var _btnClose:TextButtonColor;
		
		public function SPResDetail()
		{
			super(this);
			
			_container = new Sprite();
			addChild(_container);
			
			_areaBG = new Shape();
			_container.addChild(_areaBG);
			
			_coverLo = new Loader();
			_container.addChild(_coverLo);
			
			var tfmTitle:TextFormat = new TextFormat(FontProxy.FONT_YAHEI_BOLD, 16, 0x000000);
			tfmTitle.align = TextFormatAlign.LEFT;
			var tfmContent:TextFormat = new TextFormat(FontProxy.FONT_YAHEI, 12, 0x000000);
			tfmContent.align = TextFormatAlign.LEFT;
			
			_txtTitle = TextFieldUtil.GenerateWithTFM("", tfmTitle);
			_txtAuthor = TextFieldUtil.GenerateWithTFM("", tfmContent);
			_txtPublisher = TextFieldUtil.GenerateWithTFM("", tfmContent);
			_txtPublishDate = TextFieldUtil.GenerateWithTFM("", tfmContent);
			_txtStar = TextFieldUtil.GenerateWithTFM("", tfmContent);
			_lblBrief = TextFieldUtil.GenerateWithTFM("", tfmContent);
			_txtBrief = TextFieldUtil.GenerateWithTFM("", tfmContent, true, true, true, TextFieldAutoSize.LEFT, true);
			
			_container.addChild(_txtTitle);
			_container.addChild(_txtAuthor);
			_container.addChild(_txtPublisher);
			_container.addChild(_txtPublishDate);
			_container.addChild(_txtStar);
			_container.addChild(_lblBrief);
			_container.addChild(_txtBrief);
			
			_btnDown = new TextButtonColor("下载", 0xc6c5c5, 0x3b6893, 0x000000, 0xffffff, 8);
			_btnDown.id = "Download";
			_btnClose = new TextButtonColor("关闭", 0xc6c5c5, 0x3b6893, 0x000000, 0xffffff, 8);
			_btnClose.id = "Close";
			_container.addChild(_btnDown);
			_container.addChild(_btnClose);
			
			_coverLo.contentLoaderInfo.addEventListener(Event.COMPLETE, OnCoverLoaded);
			addEventListener(MouseEvent.CLICK, OnClick);
		}
		
		/* Event Handler
		 * ------------------------------------------------------------ */
		private function OnCoverLoaded(evt:Event):void{
			_coverWidth = evt.target.width;
			_coverHeight = evt.target.height;
			SetLayout(_areaW, _areaH);
		}
		
		private function OnClick(evt:MouseEvent):void{
			if(evt.target is TextButtonColor){
				var btn:TextButtonColor = evt.target as TextButtonColor;
				switch(btn.id){
					case "Download":
						dispatchEvent(new OnlineResEvent(DOWNLOAD, true, false, _vo));
						break;
					case "Close":
						dispatchEvent(new DataEvent(CLOSE, true, false, ""));
						break;
				}
			}
		}
		
		public function UpdateContent(vo:OnlineResVO):void{
			_vo = vo;
			_coverLo.load(new URLRequest(vo.CoverURL));
			
			_txtTitle.text = vo.Name;
			_txtAuthor.text = "作者： " + vo.Author;
			_txtPublisher.text = "出版社： " + vo.Publisher;
			_txtPublishDate.text = "出版日期： " + vo.PublishDate.toString();
			_lblBrief.text = "内容简介";
			_txtStar.text = "评分：" + vo.Star;
			_txtBrief.text = vo.Description;
		}
		
		override public function SetLayout(areaWidth:int, areaHeight:int):void{
			_areaW = areaWidth;
			_areaH = areaHeight;
			
			// 此处覆盖像类中的实现，父类中的背景只是只是用于调试时显示 SPBase 中的尺寸 
			_bg.graphics.clear();
			_bg.graphics.beginFill(0x000000, 0.65);
			_bg.graphics.drawRect(0,0,areaWidth,areaHeight);
			
			var panelW:int = int(areaWidth*0.6);
			var panelH:int = int(areaHeight*0.5);
			_areaBG.graphics.clear();
			_areaBG.graphics.beginFill(0xf1f1f1, 1.0);
			_areaBG.graphics.drawRoundRect(0,0, panelW, panelH, 10, 10);
			
			_container.x = int((areaWidth - panelW)/2);
			_container.y = int((areaHeight - panelH)/2);
			
			_coverLo.x = 28;
			_coverLo.y = 28;
			
			var txtInfoLeft:int = 28 + _coverWidth + 15;
			var insertY:int = 28;
			_txtTitle.x = txtInfoLeft;
			_txtTitle.y = insertY;	insertY += 25;
			_txtAuthor.x = txtInfoLeft;
			_txtAuthor.y = insertY;	insertY += 20;
			_txtPublisher.x = txtInfoLeft;
			_txtPublisher.y = insertY;	insertY += 20;
			_txtPublishDate.x = txtInfoLeft;
			_txtPublishDate.y = insertY;	insertY += 20;
			_txtStar.x = txtInfoLeft;
			_txtStar.y = insertY;
			
			var txtBriefTop:int = 28 + _coverHeight;
			_lblBrief.x = 28;
			_lblBrief.y = txtBriefTop + 10;
			_txtBrief.x = 28;
			_txtBrief.y = txtBriefTop + 10 + 20;
			_txtBrief.width = panelW - 50;
			_txtBrief.height = panelH - _txtBrief.y - 20;
			
			_btnClose.x = _btnDown.x = int(panelW - _btnDown.width - 20);
			_btnDown.y = 28;
			_btnClose.y = int(_btnDown.y + _btnDown.height + 10);
		}
		
		override public function Dispose():void{
			super.Dispose();
			_coverLo.contentLoaderInfo.removeEventListener(Event.COMPLETE, OnCoverLoaded);
			removeEventListener(MouseEvent.CLICK, OnClick);
		}
	}
}